import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.sql.SQLException;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Vector;

import javax.naming.NamingException;

import unilearn.domain.*;
import unilearn.facade.*;

/**
 * @author Yin Zhang, 5393558
 * @version 0.1, 07 May 2008, initial creation
 */
public class StudentUserStory
{
	private static PrintWriter fileOutput = null;
	private static final SimpleDateFormat DATEFORMAT = new SimpleDateFormat(
			"dd/MM/yyyy 'at' HH:mm:ss");
	private static final String SEPARATELINE = "--------------------------------------------------------";

	public StudentUserStory() throws FileNotFoundException
	{
		fileOutput = new PrintWriter(new FileOutputStream("studentViewAssessment.txt"));
	}

	@SuppressWarnings("deprecation")
	public void printStudents() throws SQLException, NamingException
	{
		String[] students = SubmissionFacade.getStudentNumbers();
		for (String studentNumber : students)
		{
			fileOutput.println(studentNumber);
		}
	}

	public void checkSudentNumber(String studentNumber) throws SQLException, NamingException
	{
		Vector<Submission> subs = SubmissionFacade.getSubmissions(studentNumber);

		if (subs.size() > 0)
		{
			for (Submission sub : subs)
			{
				Assessment assessment = AssessmentFacade.getBySubmissionID(sub.getSubmissionID());

				if (assessment != null)
				{
					fileOutput.println("Provisional Marks for:");
					fileOutput.println("Student: \t\t" + studentNumber);
					printAssignment(sub);
					printSubmission(sub);
					printAssessment(assessment);
				}
				else
				{
					fileOutput.print("All submissions for student ");
					fileOutput.print(studentNumber);
					fileOutput.print(" haven't been assessed yet");
				}
			}
		}

		else
		{
			fileOutput.println("Studnet Number doesn't exist in the database");
		}
	}

	private void printAssignment(Submission submission) throws SQLException, NamingException
	{
		Assignment assignment = null;
		Subject subject = null;
		String assignmentName, subjectCode, subjectName;

		assignment = AssignmentFacade.getAssignmentBySubmission(submission);
		assignmentName = assignment.getName();

		subject = SubjectFacade.getSubjectByID(assignment.getSubjectID());
		subjectCode = subject.getSubjectCode();
		subjectName = subject.getSubjectName();

		fileOutput.println("Subject: \t\t" + subjectCode + " - " + subjectName);
		fileOutput.println("Assessment: \t" + assignmentName);
		fileOutput.print(assignmentName);
	}

	private void printSubmission(Submission submission)
	{
		String submittedDate = formatDate(submission.getDateSubmitted());

		fileOutput.print(" submitted on " + submittedDate);
		fileOutput.println();
	}

	private void printAssessment(Assessment assessment) throws SQLException, NamingException
	{
		long assessmentID = assessment.getAssessmentID();
		Vector<AssessmentCriteriaValue> criteriaValues = AssessmentCriteriaValueFacade.getCriteriasByAssessment(assessmentID);
		int maxMark = 0;
		int totalMark = 0;
		fileOutput.println("Tutor marking details:");

		for (AssessmentCriteriaValue criteriaValue : criteriaValues)
		{
			AssessmentCriteria assCr = AssessmentCriteriaFacade.getCriteriaByID(criteriaValue.getCriteriaID());
			int criteriaMaxGrade = assCr.getCriteriaMaxGrade();
			int criteriaGrade = criteriaValue.getGrade();
			maxMark = maxMark + criteriaMaxGrade;
			totalMark = totalMark + criteriaGrade;
			fileOutput.print(assCr.getCriteriaName());
			fileOutput.print("\t\t\t | \t\t\t");
			fileOutput.print(criteriaGrade + " / " + criteriaMaxGrade);
			fileOutput.println();
			fileOutput.println(assCr.getCriteriaDescription());
			fileOutput.println(SEPARATELINE);
		}
		fileOutput.println("Comments:");
		fileOutput.println(assessment.getComment());
		fileOutput.println(SEPARATELINE);
		fileOutput.println("Total Mark: \t\t\t" + totalMark + " / " + maxMark);
		fileOutput.println(SEPARATELINE);
		fileOutput.println("Late penalty = Not Applicable");
		fileOutput.println(SEPARATELINE);
		fileOutput.println("Final Mark: \t\t\t" + assessment.getGrade());
	}

	private static String formatDate(Timestamp timestamp)
	{
		if (timestamp == null)
		{
			return null;
		}
		else
		{
			return DATEFORMAT.format((java.util.Date) timestamp);
		}
	}

	public void close()
	{
		try
		{
			fileOutput.close();
		}
		finally
		{
			fileOutput = null;
		}
	}
}